<!--
 * @Description: 排放源管理
 * @Author: qingyang
 * @Date: 2022-01-10 16:04:17
 * @LastEditors: qingyang
 * @LastEditTime: 2023-07-20 10:36:16
-->
<template>
  <div class="emission-container">
    <CPageTable @register="registerPage" v-model:searchForm="searchForm">
      <template #headerRightButton>
        <el-button v-btnPermission="'ECI_EMISSION_SOURCE_ADD'" type="primary" @click="addTask"
          ><CIcon type="c-xian-xinjian" class="g-mr4"></CIcon>新增</el-button
        >
      </template>
      <template #nameSlot="{ scope }">
        <CTableColumn :main-text="scope.row.name" :sub-text="scope.row.sourceNo"></CTableColumn>
      </template>
      <template #typeSlot="{ scope }">
        {{ empty(emissionTypeEnum[scope.row.type as keyof typeof emissionTypeEnum]) }}
      </template>
      <template #modifiedBySlot="{ scope }">
        <CTableColumn
          :main-text="scope.row.modifiedBy"
          :sub-text="scope.row.updateTime"
        ></CTableColumn>
      </template>
      <template #statusSlot="{ scope }">
        <el-tag :type="renderTag(scope.row.status)">{{
          scope.row.status == 'ENABLE' ? '已启用' : '已停用'
        }}</el-tag>
      </template>
      <template #op="{ scope }">
        <CTableButtonGroup :buttons-config="getTableButtonsConfig(scope.row)" />
      </template>
    </CPageTable>
    <add-or-edit-task-dialog
      v-model:visible="addOrEditTaskVisible"
      :info="infoDetail"
      @refresh="handleRefresh"
    ></add-or-edit-task-dialog>
    <update-list v-model:visible="updateListVisible" :info="updateListInner"></update-list>
  </div>
</template>
<script lang="ts">
export default {
  name: 'Emission'
};
</script>
<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { useFactorList } from './useFactorList';
import { empty } from '@/utils/index';
import { emissionTypeEnum } from '@/constants/enum';
import { useMessage } from '@/hooks/useMessage';
import AddOrEditTaskDialog from './components/AddOrEditTaskDialog/index.vue';
import { updateStatus, delEmission } from '@/apis/system/config/emissionList';
import updateList from './components/updateList/index.vue';
import { CPageTable, useCPageTable, CPageTableProps } from '@/components/CPageTable';
import { CTableColumn, CTableButtonGroup } from '@/components/CTable';
import type { TableButtonItem } from '@/components/CTable';
import { GetEmissionListResult } from '@/apis/system/config/model/emissionListModel';
const { createConfirm, createMessage } = useMessage();
const { searchOptions, searchForm, tableOptions, tableFieldsSetting } = useFactorList();
const addOrEditTaskVisible = ref(false);
const infoDetail = ref();
const updateListVisible = ref(false);
const updateListInner = ref();

const options = reactive<CPageTableProps>({
  title: '排放源配置',
  searchOptions: searchOptions,
  tableOptions,
  tableFieldsSetting,
  enableFieldsSetting: true
});

const [registerPage, { refresh }] = useCPageTable(options);

const handleRefresh = () => {
  refresh();
};
const renderTag = (status: string) => {
  switch (status) {
    case 'ENABLE':
      return 'success';
    default:
      return 'info';
  }
};

const addTask = () => {
  infoDetail.value = undefined;
  addOrEditTaskVisible.value = true;
};

const handelUpdate = (row: GetEmissionListResult) => {
  let flag = row.status;
  createConfirm(
    flag == 'ENABLE' ? '确认停用？' : '确认启用？',
    flag == 'ENABLE'
      ? '当前数据停用后，将无法在系统中进行匹配使用，归档数据不受影响。'
      : '确认启用当前数据。',
    {
      iconType: flag == 'ENABLE' ? 'warning' : '',
      confirmButtonText: flag == 'ENABLE' ? '停用' : '启 用'
    }
  ).then(async () => {
    updateStatus({
      sourceNo: row.sourceNo as string,
      status: row.status == 'ENABLE' ? 'DISABLE' : 'ENABLE'
    }).then((res: any) => {
      createMessage.success('成功修改状态！');
      refresh();
    });
  });
};
const handleEdit = (row: GetEmissionListResult) => {
  infoDetail.value = row;
  addOrEditTaskVisible.value = true;
};

const handleDel = (row: GetEmissionListResult) => {
  createConfirm('提示', '确认删除该排放源？', {
    iconType: 'warning',
    confirmButtonText: '删除'
  }).then(() => {
    delEmission(row.sourceNo).then(() => {
      createMessage.success('删除成功！');
      refresh();
    });
  });
};

/**
 * 配置表格的按钮
 * @param row 表格行数据
 */
function getTableButtonsConfig(row: any): TableButtonItem[] {
  return [
    {
      text: '编辑',
      btnPermission: 'ECI_EMISSION_SOURCE_EDIT',
      onClick: () => handleEdit(row)
    },

    {
      text: row.status == 'ENABLE' ? '停用' : '启用',
      btnPermission: 'ECI_EMISSION_SOURCE_UPDATE',
      onClick: () => handelUpdate(row)
    },
    {
      text: '删除',
      btnPermission: 'ECI_EMISSION_SOURCE_DELETE',
      onClick: () => handleDel(row)
    }
  ];
}
</script>
